
const { generateToken } = require('../../utils/middleware')
const {userledger,sequelize,salesmanInfo,merchantInfo,memberInfo} = require('../../dbConfig/index.js')
// 登录
const login = async (ctx,next)=>{
	const {userName,user_password,is_mini} = ctx.request.body
	if(!(userName&&user_password)){
		ctx.body = {
			code:1,
			data:null,
			msg:'请输入用户名或密码'
		}
		return
	}
	let userData = await userledger.findOne({where: {user_name: userName,user_password:user_password}})
	let res={}
	if(!userData){
		ctx.body = {code:1,data:null,msg:'用户名或密码错误'}
		return
	}
	if(is_mini){
		console.log(userData,'xiaochengxu')
		// TODO  小程序用户名密码登录 只能查商家或会员
		if(userData.is_flg=='1' || userData.is_flg=='3'){
			ctx.body = {
				code:1,
				data:null,
				msg:'用户名或密码错误'
			}
			return
		}

	}
	
	res = {
		token:generateToken({user_id:userData.Id}),
		create_id:userData.creator_id,
		flg:userData.is_flg
	}
	
	
	ctx.body = {
		code:200,
		data:res,
		msg:'登录成功'
	}
}

const info = async(ctx,next)=>{
	const body =ctx.request.body
	const user_id = body.create_id||ctx.state.user_id
	let res = {}
	let code = 200
	let msg = body.is_flg==='1'?'超级管理员':'成功'
	if(body.is_flg==='2'){// 商家
		res = await merchantInfo.findOne({where:{user_id:user_id}})
		if(res.payment_status==='2'){
			code=1
			msg='该账户目前正在加急审核中,请稍等'
		}
	}else if(body.is_flg==='3'){
		res = await salesmanInfo.findOne({where:{user_id:user_id}})
	}else if(body.is_flg==='4'){
		res = await memberInfo.findOne({where:{user_id:user_id}})
	}  
	
		ctx.body = {
			code:code,
			data:res,
			msg:msg
		}
	
}
const updataVersion = async(ctx,next)=>{
	
}
module.exports = {
	login,
	info,
	// updataUserInfo,
	// checkUser,
	// getmerchantList
}